//$URL: http://dasding.googlecode.com/svn/branches/testzeugs/data/data/src/main/java/de/piratech/dasding/data/Event.java $
//$Id: Event.java 89 2012-11-12 14:55:42Z amuthmann@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.data;

import de.micromata.opengis.kml.v_2_2_0.Document;
import de.micromata.opengis.kml.v_2_2_0.Folder;
import de.piratech.dasding.data.database.DataType;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.annotate.JsonIgnore;

/**
 * An event
 *
 * @author deveth0
 */
@XmlRootElement
public class Event extends AbstractAction {

  /**
   * The different event status
   */
  public enum EVENTSTATUS {

    /**
     * Active event
     */
    ACTIVE,
    /**
     * Canceled event
     */
    CANCELED,
    /**
     * Event is over
     */
    FINISHED
  }
  private List<Action> mActions = new ArrayList<Action>();
  private List<String> mProspects = new ArrayList<String>();
  private EVENTSTATUS mStatus = EVENTSTATUS.ACTIVE;
  private String mPredecessor;
  private String mCategory;

  @XmlElementWrapper
  @XmlElement(name = "action")
  public List<Action> getActions() {
    return mActions;
  }

  public void addAction(Action pAction) {
    mActions.add(pAction);
  }

  public void setActions(List<Action> pActions) {
    this.mActions = pActions;
  }

  @XmlElementWrapper
  public List<String> getProspects() {
    return mProspects;
  }

  public void setProspects(List<String> pProspects) {
    this.mProspects = pProspects;
  }

  public EVENTSTATUS getStatus() {
    return mStatus;
  }

  public void setStatus(EVENTSTATUS pStatus) {
    this.mStatus = pStatus;
  }

  public String getPredecessor() {
    return mPredecessor;
  }

  public void setPredecessor(String pPredecessor) {
    this.mPredecessor = pPredecessor;
  }

  public String getCategory() {
    return mCategory;
  }

  public void setCategory(String pCategory) {
    this.mCategory = pCategory;
  }

  @Override
  @JsonIgnore
  public boolean isValid() {
    return super.isValid() && mCategory != null;
  }

  @JsonIgnore
  public Document getKML() {
    final Document document = new Document();
    document.createAndAddPlacemark().withName(getName())
            .withDescription(getDescription())
            .createAndSetPoint().addToCoordinates(getLocation().getLongitude(), getLocation().getLatitude());

    if (!getActions().isEmpty()) {
      Folder acts = document.createAndAddFolder().withName("actions");
      for (Action action : getActions()) {
        acts.createAndAddPlacemark().withName(action.getName())
                .withDescription(action.getDescription())
                .createAndSetPoint().addToCoordinates(action.getLocation().getLongitude(), action.getLocation().getLatitude());
      }
    }
    return document;
  }

  @Override
  public DataType getDataType() {
    return super.getDataType() != null ? super.getDataType() : DataType.EVENT;
  }
}
